<?php

/*
 * author: P.L.A
 * create: 2017-08-23
 * */

namespace app\admin\model;
use think\Db;
use think\Model;
use think\Request;


class User extends Model
{
    public $table = 'am_user';

    public $pk = 'id';

    /*获取会员列表方法*/
    public function showMember($keyword){
        if( $keyword != '' ){
            $list = Db::table($this->table)
                ->order($keyword)
                ->select();
        }else{
            $list = Db::table($this->table)
                ->order('id desc')
                ->select();
        }
        return $list;
    }


    /*添加会员方法*/
    public function addMember($data){

        $data['mobile'] = (int)$data['mobile'];
        $data['create_at'] = time();
        $data['last_login_ip'] = getIP();
        $data['img'] == '' ? $data['img'] = "__PUBLIC__/image/default.png" :$data['img'] = upload();
        $res = Db::table($this->table)
            ->insert($data);
        return $res;
    }

    public function postEdit($id,$data){
        $data['edit_ip'] = getIP();
        $data['edit_at'] = time();
        $res = Db::table($this->table)
            ->where('id',$id)
            ->update($data);
        return $res;
    }


    public function delMember($id){
        $img_src_tmp = Db::table($this->table)
            ->find($id);
        $img_src = $img_src_tmp['img'];
        $dir = sub_str($img_src,0,8);
        $file = sub_str($img_src,9);
        $res = Db::table($this->table)->delete($id);
        if( $res ){
            $unlink = delDir($img_src);
        }


    }
}